const user = require("../../database/user")
const md5 = require("md5");

module.exports = async (req,res,next)=>{
  const {userId, opassword, npassword} = req.body;

  if(!userId || !opassword || !npassword){
    res.send({
      code:2,
      msg:"缺少必传参数：userId 或 opassword 或 npassword"
    })
    return;
  }

  const data = await user.find( {userId} );
  if(data.length < 1){
    res.send({
      code:0,
      msg:"不存在该用户，userId有误"
    })
  }else if(data[0].password === md5(opassword)){
    await user.updateOne( {userId} , {password: md5(npassword)})
    res.send({
      code:1,
      msg:"密码修改成功"
    })
  }else{
    res.send({
      code:3,
      msg:"原密码不对"
    })
  }
}